package com.rew.canteenbox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.rew.canteenbox.R;
import com.rew.canteenbox.bases.AppData;
import com.rew.canteenbox.models.Dishes;

import java.util.List;

/**
 *  菜单界面Adapter设置.
 *
 * Created by HuiWen Ren on 2016/10/18.
 */
public class DishesMenuAdapter extends ArrayAdapter<Dishes>{

    private int resourceId;
    private Dishes dishes;
    private View view;
    private AppData appData;

    public DishesMenuAdapter(Context context, int textViewResourceId,
                         List<Dishes> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
        appData = (AppData) getContext().getApplicationContext() ;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        dishes = getItem(position); // 获取当前项的Dishes实例.
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            // 匹配元件
            viewHolder = new ViewHolder();
            viewHolder.dishesImage = (ImageView) view.findViewById
                    (R.id.dish_img);
            viewHolder.dishesName = (TextView) view.findViewById
                    (R.id.dish_name_tx);
            viewHolder.dishesPrice = (TextView) view.findViewById
                    (R.id.dish_price_tx);
            viewHolder.dishNum = (TextView) view.findViewById
                    (R.id.tv_item_num);
            viewHolder.addBtn = (Button) view.findViewById
                    (R.id.list_add_btn);
            viewHolder.minBtn = (Button) view.findViewById
                    (R.id.list_minus_btn);
            view.setTag(viewHolder); // 将ViewHolder存储在View中.
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder.
        }
        viewHolder.dishesImage.setImageResource(dishes.getPicId());
        viewHolder.dishesName.setText(dishes.getDishName());
        viewHolder.dishesPrice.setText("￥" + dishes.getDishPrice());

        viewHolder.freshView(appData.getNum(getItem(position)));

        // 点击当前position(Tag) 从appData中取出数据刷新View
        viewHolder.addBtn.setTag(position);
        viewHolder.addBtn.setOnClickListener(v -> {
            appData.addOrder(getItem(position));
            int num =appData.getNum(getItem(position));
            viewHolder.dishNum.setText(Integer.toString(num));
            viewHolder.freshView(num);
            //animateClickView(v, v1 -> appData.addOrder(getItem(position)));

        });

        // 点击当前position(Tag) 从appData中取出数据刷新View
        viewHolder.minBtn.setTag(position);
        viewHolder.minBtn.setOnClickListener(v -> {
            if(appData.getNum(getItem(position)) > 0){
                appData.minusOrder(getItem(position));
                int num =appData.getNum(getItem(position));
                viewHolder.dishNum.setText(Integer.toString(num));
                viewHolder.freshView(num);
                //animateClickView(v, v1 -> appData.minusOrder(getItem(position)));
            }
        });

        viewHolder.dishNum.setText(Integer.toString(appData.getNum(getItem(position))));
        return view;
    }


    class ViewHolder {
        ImageView dishesImage;
        TextView dishesName;
        TextView dishesPrice;
        TextView dishNum;
        Button addBtn;
        Button minBtn;

        private void freshView(int num){
            if(num == 0){
                dishNum.setVisibility(View.GONE);
                minBtn.setVisibility(View.GONE);
                //notifyDataSetChanged();
            } else {
                dishNum.setVisibility(View.VISIBLE);
                minBtn.setVisibility(View.VISIBLE);
                //notifyDataSetChanged();
            }
        }
    }
}
